package com.yanqu.backendweb.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.yanqu.backendweb.bean.LogDto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.collections.CollectionUtils;

import java.util.Collection;

/**
 * @author duli
 * @version 1.0
 * @date 2020/9/18 14:22
 * @Description
 */
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response<T> {
    private static final Response SUCCESS_EMPTY_DATA = new Response(eErrorCode.SUCCESS);
    private static final Response SIGN_ERROR = new Response(eErrorCode.SIGN_ERROR);

    private int status;

    private String msg;

    private T data;

    public Response(eErrorCode errorCode) {
        this.status = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }

    public Response(eErrorCode errorCode, T data) {
        this.status = errorCode.getCode();
        this.msg = errorCode.getMsg();
        this.data = data;
    }

    public Response(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public static Response success() {
        return SUCCESS_EMPTY_DATA;
    }

    public static <T> Response<T> success(T data) {
        return new Response<>(eErrorCode.SUCCESS, data);
    }
    public static Response singError() {
        return new Response<>(eErrorCode.SIGN_ERROR, null);
    }


    @JsonIgnore
    @JSONField(serialize = false)
    public boolean isSuccess() {
        return status == eErrorCode.SUCCESS.getCode();
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }


}
